package com.flyco.modules.oms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.oms.entity.OmsOrderPriceDistance;
import com.flyco.modules.oms.service.IOmsOrderPriceDistanceService;

import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.poi.excel.ExcelImportUtil;
import com.flyco.poi.excel.def.NormalExcelConstants;
import com.flyco.poi.excel.entity.ExportParams;
import com.flyco.poi.excel.entity.ImportParams;
import com.flyco.poi.excel.view.EntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 订单起订金额设置
 * @Author: flyco
 * @Date: 2023-07-14
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "订单起订金额设置")
@RestController
@RequestMapping("/f/oms/omsOrderPriceDistance")
public class OmsOrderPriceDistanceController extends BaseController<OmsOrderPriceDistance, IOmsOrderPriceDistanceService> {
    @Autowired
    private IOmsOrderPriceDistanceService omsOrderPriceDistanceService;

    /**
     * 分页列表查询
     *
     * @param omsOrderPriceDistance
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "订单起订金额设置-分页列表查询")
    @ApiOperation(value = "订单起订金额设置-分页列表查询", notes = "订单起订金额设置-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(OmsOrderPriceDistance omsOrderPriceDistance,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {

        LambdaQueryWrapper<OmsOrderPriceDistance> queryWrapper = new LambdaQueryWrapper<>();
        IPage<OmsOrderPriceDistance> pageList = new Page<>();
        if (StringUtils.isEmpty(omsOrderPriceDistance.getChannelId())
                && StringUtils.isEmpty(omsOrderPriceDistance.getStoreId())) {
            return Result.ok(pageList);
        }
        if(!StringUtils.isEmpty(omsOrderPriceDistance.getChannelId())){
            queryWrapper.eq(OmsOrderPriceDistance::getChannelId, omsOrderPriceDistance.getChannelId());
        }
        if(!StringUtils.isEmpty(omsOrderPriceDistance.getStoreId())){
            queryWrapper.eq(OmsOrderPriceDistance::getStoreId, omsOrderPriceDistance.getStoreId());
        }
        queryWrapper.orderByAsc(OmsOrderPriceDistance::getMinimumDistance);
        Page<OmsOrderPriceDistance> page = new Page<OmsOrderPriceDistance>(pageNo, pageSize);
        pageList = omsOrderPriceDistanceService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param omsOrderPriceDistance
     * @return
     */
    @AutoLog(value = "订单起订金额设置-添加")
    @ApiOperation(value = "订单起订金额设置-添加", notes = "订单起订金额设置-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody OmsOrderPriceDistance omsOrderPriceDistance) {
        omsOrderPriceDistanceService.save(omsOrderPriceDistance);
        return Result.ok("添加成功！");
    }

    /**
     * 添加
     *
     * @param omsOrderPriceDistance
     * @return
     */
    @AutoLog(value = "订单起订金额设置-添加")
    @ApiOperation(value = "订单起订金额设置-添加", notes = "订单起订金额设置-添加")
    @PostMapping(value = "/addBatch")
    public Result<?> addBatch(@RequestBody List<OmsOrderPriceDistance> omsOrderPriceDistance) {
        omsOrderPriceDistanceService.saveOrUpdateBatch(omsOrderPriceDistance);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param omsOrderPriceDistance
     * @return
     */
    @AutoLog(value = "订单起订金额设置-编辑")
    @ApiOperation(value = "订单起订金额设置-编辑", notes = "订单起订金额设置-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody OmsOrderPriceDistance omsOrderPriceDistance) {
        omsOrderPriceDistanceService.updateById(omsOrderPriceDistance);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "订单起订金额设置-通过id删除")
    @ApiOperation(value = "订单起订金额设置-通过id删除", notes = "订单起订金额设置-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        omsOrderPriceDistanceService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "订单起订金额设置-批量删除")
    @ApiOperation(value = "订单起订金额设置-批量删除", notes = "订单起订金额设置-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.omsOrderPriceDistanceService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "订单起订金额设置-通过id查询")
    @ApiOperation(value = "订单起订金额设置-通过id查询", notes = "订单起订金额设置-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        OmsOrderPriceDistance omsOrderPriceDistance = omsOrderPriceDistanceService.getById(id);
        return Result.ok(omsOrderPriceDistance);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param omsOrderPriceDistance
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OmsOrderPriceDistance omsOrderPriceDistance) {
        return super.exportXls(request, omsOrderPriceDistance, OmsOrderPriceDistance.class, "订单起订金额设置");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OmsOrderPriceDistance.class);
    }

}
